﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bamboo.Excel
{
    public class MultiListToExcel
    {
        NPOI.XSSF.UserModel.XSSFWorkbook _workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();

        public string FileName { get; }

        public MultiListToExcel(params string[] pathList) 
        {
            this.FileName = Path.Combine(pathList);
        }

        public void DeleteFile()
        {
            if (System.IO.File.Exists(FileName))
            {
                System.IO.File.Delete(FileName);
            }
        }

        public void ListToSheet<T>(IEnumerable<T> list, string sheetName)
            where T : class
        {
            var sheet = _workbook.CreateSheet(sheetName);
            Bamboo.Excel.NPOIEx.ListToSheet(sheet, list);
        }

        public void Save()
        {
            using (var fs = System.IO.File.OpenWrite(FileName))
            {
                _workbook.Write(fs);
                fs.Close();
            }
        }
    }
}
